$(function() {
    BJUI.init({
        JSPATH       : 'B-JUI/',         //[可选]框架路径
        PLUGINPATH   : 'B-JUI/plugins/', //[可选]插件路径
        loginInfo    : {url:'login_timeout.html', title:'登录', width:440, height:240}, // 会话超时后弹出登录对话框
        statusCode   : {ok:200, error:300, timeout:301}, //[可选]
        ajaxTimeout  : 300000, //[可选]全局Ajax请求超时时间(毫秒)
        alertTimeout : 3000,  //[可选]信息提示[info/correct]自动关闭延时(毫秒)
        pageInfo     : {total:'totalRow', pageCurrent:'pageCurrent', pageSize:'pageSize', orderField:'orderField', orderDirection:'orderDirection'}, //[可选]分页参数
        keys         : {statusCode:'statusCode', message:'message'}, //[可选]
        ui           : {
            sidenavWidth     : 200,
            showSlidebar     : true, //[可选]左侧导航栏锁定/隐藏
            overwriteHomeTab : false //[可选]当打开一个未定义id的navtab时，是否可以覆盖主navtab(我的主页)
        },
        debug        : true,    // [可选]调试模式 [true|false，默认false]
        theme        : 'green' // 若有Cookie['bjui_theme'],优先选择Cookie['bjui_theme']。皮肤[五种皮肤:default, orange, purple, blue, red, green]
    })
    //时钟
    var today = new Date(), time = today.getTime()
    $('#bjui-date').html(today.formatDate('yyyy/MM/dd'))
    setInterval(function() {
        today = new Date(today.setSeconds(today.getSeconds() + 1))
        $('#bjui-clock').html(today.formatDate('HH:mm:ss'))
    }, 1000)
})

/*window.onbeforeunload = function(){
 return "确定要关闭本系统 ?";
 }*/

//菜单-事件-zTree
function MainMenuClick(event, treeId, treeNode) {
    alert(treeNode.targetid+treeNode.url)
    if (treeNode.target && treeNode.target == 'dialog' || treeNode.target == 'navtab')
        event.preventDefault()

    if (treeNode.isParent) {
        var zTree = $.fn.zTree.getZTreeObj(treeId)

        zTree.expandNode(treeNode)
        return
    }

    if (treeNode.target && treeNode.target == 'dialog')
        $(event.target).dialog({id:treeNode.targetid, url:treeNode.url, title:treeNode.name})
    else if (treeNode.target && treeNode.target == 'navtab')

        $(event.target).navtab({id:treeNode.targetid, url:treeNode.url, title:treeNode.name, fresh:treeNode.fresh, external:treeNode.external})
}

// 满屏开关
var bjui_index_container = 'container_fluid'

function bjui_index_exchange() {
    bjui_index_container = bjui_index_container == 'container_fluid' ? 'container' : 'container_fluid'

    $('#bjui-top').find('> div').attr('class', bjui_index_container)
    $('#bjui-navbar').find('> div').attr('class', bjui_index_container)
    $('#bjui-body-box').find('> div').attr('class', bjui_index_container)
}

function bjui_title_url(value, data) {
    var len = value.length
    var total = 30
    var title = value
    if(len > total) {
        var dot = "..."
        var flag = true
        value = value.substring(0,total-dot.length)+dot

    }
    var temp = BJUI.StrBuilder()
    temp.add('<a href="')
        .add(data.url)
        .add('" target="_blank" title="')
        .add(title).add('" data-toggle="tooltip" data-placement="bottom">')
        .add(value).add('</a>')
    return temp.toString()
}

function bjui_department_click(event, treeId, treeNode) {
   if (treeNode.isParent) {
        var zTree = $.fn.zTree.getZTreeObj(treeId)
        zTree.expandNode(treeNode)
    }
    /*BJUI.ajax('doajax', {
        url: 'system/organization/dept/tree',
        data: {parentId: treeNode.id},
        loadingmask: true,
        target: $("#datagrid-department"),
        datagrids:'datagrid-department'
    })*/
    $('#datagrid-users').datagrid('filter', {parentId: treeNode.id})
    event.preventDefault()
}

function bjui_user_toolbar() {
    return '<button type="button" class="btn-green" data-icon="home">测试<button>'
}